package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/15 10:31
 * @Desc:
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public List<ShoppingCart> listAll(HttpServletRequest request) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(null);
        if (shoppingCarts.size() != 0)
            request.getSession().setAttribute("shoppingCart", shoppingCarts.get(0));

        return shoppingCarts;
    }

    @Override
    public void addShoppingCart(ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public void clearShoppingCart() {
        for (ShoppingCart shoppingCart : shoppingCartMapper.selectList(null)) {
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }
    }

    @Override
    public void subShoppingCart(ShoppingCart shoppingCart) {
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", shoppingCart.getSetmealId());

//        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(wrapper);

        shoppingCartMapper.delete(wrapper);
    }
}
